package gui.panels;

import com.google.common.eventbus.Subscribe;
import contracts.requests.RegisterRequest;
import contracts.responses.LoginResponse;
import contracts.responses.RegisterResponse;
import eventbus.PageEventBus;
import eventbus.PageEvents;
import eventbus.events.UpdatePanelEvent;
import gui.BasePanel;
import network.websocket.WSClientSender;
import util.DialogHelper;
import util.StringValidator;

import javax.swing.*;
import java.awt.*;

public class RegisterPanel extends BasePanel {

    JLabel titleLabel = new JLabel("注册");

    JLabel nicknameLabel = new JLabel("昵称:");
    JTextField nicknameInput = new JTextField();

    JLabel usernameLabel = new JLabel("用户名:");
    JTextField usernameInput = new JTextField();

    JLabel passwordLabel = new JLabel("密码:");
    JPasswordField passwordInput = new JPasswordField();

    JButton registerButton = new JButton("注册");
    JButton backLoginButton = new JButton("返回登录");

    public RegisterPanel() {
        //800-80
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 40));
        titleLabel.setForeground(Color.blue);
        titleLabel.setBounds(360, 50, 80, 50);

        //235
        nicknameLabel.setFont(new Font("微软雅黑", Font.BOLD, 25));
        nicknameLabel.setForeground(Color.red);
        nicknameLabel.setBounds(200, 150, 130, 32);
        nicknameInput.setBounds(330, 150, 230, 32);


        usernameLabel.setFont(new Font("微软雅黑", Font.BOLD, 25));
        usernameLabel.setForeground(Color.red);
        usernameLabel.setBounds(200, 200, 130, 32);
        usernameInput.setBounds(330, 200, 230, 32);


        passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 25));
        passwordLabel.setForeground(Color.red);
        passwordLabel.setBounds(200, 250, 130, 32);
        passwordInput.setBounds(330, 250, 230, 32);


        registerButton.setBackground(Color.green);
        registerButton.setBounds(300, 300, 200, 32);
        backLoginButton.setBackground(Color.ORANGE);
        backLoginButton.setBounds(300, 350, 200, 32);


        this.add(titleLabel);
        this.add(nicknameLabel);
        this.add(nicknameInput);
        this.add(usernameLabel);
        this.add(usernameInput);
        this.add(passwordLabel);
        this.add(passwordInput);
        this.add(registerButton);
        this.add(backLoginButton);

        backLoginButton.addActionListener(e -> {
            PageEventBus.publish(UpdatePanelEvent.create(LoginPanel.class));
        });
        registerButton.addActionListener(e -> {
            String userName = usernameInput.getText().trim();
            String nickname = nicknameInput.getText().trim();
            String password = new String(passwordInput.getPassword());
            if (StringValidator.isBlank(userName)) {
                JOptionPane.showMessageDialog(null, "用户名不能为空");
                return;
            }
            if (StringValidator.isBlank(nickname)) {
                JOptionPane.showMessageDialog(null, "昵称不能为空");
                return;
            }
            if (StringValidator.isBlank(password)) {
                JOptionPane.showMessageDialog(null, "密码不能为空");
                return;
            }

            //组装请求实体
            RegisterRequest request = new RegisterRequest(userName, password, nickname);
            WSClientSender.send(request);
        });

    }

    @Subscribe
    public void handleRegisterResponse(RegisterResponse response) {
        if (response.success) {
            DialogHelper.showSuccessDialog("注册成功! 请登录");
            PageEventBus.publish(UpdatePanelEvent.create(LoginPanel.class));
        } else {
            DialogHelper.showWaringDialog(response.errorMessage);
        }
    }

    @Override
    public BasePanel clearAllInput() {
        nicknameInput.setText("");
        usernameInput.setText("");
        passwordInput.setText("");
        return this;
    }
}
